package com.bw.cinema

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import coil.compose.AsyncImage
import com.bw.cinema.entity.MutilVideoEntity
import com.bw.cinema.intent.CinemaIntent
import com.bw.cinema.viewmodel.CinemaViewModel
import com.bw.lib_miv.IUiState
import com.bw.lib_miv.RequestType
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch


@Composable
fun ProfilePage(
    entity: MutilVideoEntity,
    vm: CinemaViewModel = hiltViewModel(),
    scope: CoroutineScope = rememberCoroutineScope()
) {

    val list = remember {
        SnapshotStateList<MutilVideoEntity>()
    }


    LaunchedEffect(key1 = "", block = {
        scope.launch {
            vm.uiState.collect {
                when (it) {

                    is IUiState.OnSuccess -> {
                        if (it.type == RequestType.RECOMMEND_MUTIL_VIDEO) {
                            list.clear()
                            list.addAll(it.data as Collection<MutilVideoEntity>)
                        }
                    }

                    is IUiState.OnFailed -> {
                        Log.d("TagA", "猜您喜欢数据获取失败: ${it.msg}")
                    }

                    else -> {}
                }
            }
        }
        vm.sendIntent(CinemaIntent.getRecommendMutilVideo(1, 14))
    })


    Column(
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .padding(5.dp)
    ) {

        Row(
            Modifier
                .fillMaxWidth()
                .height(50.dp), verticalAlignment = Alignment.CenterVertically
        ) {
            AsyncImage(model = entity.avatar_url, contentDescription = "", Modifier.size(30.dp))

            Text(text = entity.name, fontSize = 18.sp)

        }
        Text(text = entity.title, fontSize = (16.sp))

        Row(
            Modifier
                .fillMaxWidth()
                .height(100.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {

            Column(
                Modifier
                    .weight(1f)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_home_detail_description_agree),
                    contentDescription = ""
                )
                Text(text = "9841", fontSize = 16.sp)
            }


            Column(
                Modifier
                    .weight(1f)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_home_detail_description_collect),
                    contentDescription = ""
                )
                Text(text = "收藏", fontSize = 16.sp)
            }


            Column(
                Modifier
                    .weight(1f)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_home_detail_description_shared),
                    contentDescription = ""
                )
                Text(text = "262", fontSize = 16.sp)
            }



            Column(
                Modifier
                    .weight(1f)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_home_detail_description_admire),
                    contentDescription = ""
                )
                Text(text = "654", fontSize = 16.sp)
            }
        }

        Text(text = "猜您喜欢", fontSize = 18.sp)

//        Text(text = "简介:", fontSize = 18.sp)
//        Text(text = entity.intro, maxLines = 6, overflow = TextOverflow.Ellipsis)

        LazyVerticalGrid(columns = GridCells.Fixed(2), content = {
            items(list) {
                guessLike(entity = it)
            }
        })

    }
}

@Composable
fun guessLike(entity: MutilVideoEntity) {

    Column(
        Modifier
            .fillMaxWidth()
            .padding(5.dp)
    ) {

        AsyncImage(
            model = entity.videomainimag, contentDescription = "", contentScale = ContentScale.Crop,
            modifier = Modifier
                .fillMaxWidth()
                .height(150.dp)
        )

        Text(text = entity.title, fontSize = 16.sp)
    }

}